import { Init, Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { GoodsPromoMzEntity } from '../entity/promo_mz';

/**
 * 促销买赠服务
 */
@Provide()
export class GoodsPromoMzService extends BaseService {
  @InjectEntityModel(GoodsPromoMzEntity)
  promoMzEntity: Repository<GoodsPromoMzEntity>;

  @Init()
  async init() {
    await super.init();
    this.setEntity(this.promoMzEntity);
  }

  /**
   * 根据单据号获取买赠明细
   * @param billNo 单据号
   */
  async getByBillNo(billNo: string) {
    return await this.promoMzEntity.find({
      where: { billNo, status: 1 },
      order: { billSn: 'ASC' }
    });
  }

  /**
   * 根据商品ID获取买赠促销
   * @param goodsId 商品ID
   */
  async getByGoodsId(goodsId: string) {
    return await this.promoMzEntity.find({
      where: { goodsId, status: 1 },
      order: { createTime: 'DESC' }
    });
  }

  /**
   * 获取所有买赠促销明细
   */
  async getAllActiveDetails() {
    return await this.promoMzEntity.find({
      where: { status: 1 },
      order: { createTime: 'DESC' }
    });
  }
} 